//
//  LXEmoticonPageViewCell.swift
//  weibo
//
//  Created by lixin on 16/4/25.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

class LXEmoticonPageViewCell: UICollectionViewCell {
    
    var buttons:[LXEmoticonButton] = [LXEmoticonButton]()
    
    //一维数组
    var emoticons: [LXEmoticon]?{
        didSet{
            
            for button in buttons{
                
                button.hidden = true
            }
            
            for (i, emoticon) in emoticons!.enumerate(){
                
                let button = buttons[i]
                
                //给自定义的button的属性赋值
                button.emoticon = emoticon
                
                button.hidden = false
                
                if emoticon.isEmoji{
                    
                    button.setTitle(((emoticon.code ?? "") as NSString).emoji(), forState: UIControlState.Normal)
                    
                    //解决cell重用问题
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                }else{
                   
                    let path = "\(emoticon.path ?? "")\(emoticon.png ?? "")"
                   
                    let image = UIImage(named: path, inBundle: LXEmoticonTools.sharedTools.emoticonBundle, compatibleWithTraitCollection: nil)
                    
                    button.setImage(image, forState: UIControlState.Normal)
                    
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
            }
        }
    }
    
    var indexPath: NSIndexPath?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 视图设置
    private func setUI(){
    
        self.backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        addChildButtons()
        
        contentView.addSubview(deleteBtn)
    }
    
    //MARK: - 添加表情按钮
    func addChildButtons(){
        
        for _ in 0..<LXEmoticonMaxCount{
            
            let button = LXEmoticonButton()
            
            button.addTarget(self, action: #selector(LXEmoticonPageViewCell.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            
            contentView.addSubview(button)
            
            buttons.append(button)
        }
    }
    
    //表情按钮点击
    @objc private func buttonClick(button: LXEmoticonButton){
    
        NSNotificationCenter.defaultCenter().postNotificationName("EmoticonButtonNoti", object: button.emoticon)
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = (SCREENW - 10) / CGFloat(LXEmoticonMaxCol)
        
        let h = (216 - 37 - 20) / CGFloat(LXEmoticonMaxRow)
        
        for (i, button) in buttons.enumerate(){
            
            let x = i % LXEmoticonMaxCol
            
            let y = i / LXEmoticonMaxCol
            
            button.frame = CGRect(x: CGFloat(x) * w + 5, y: CGFloat(y) * h, width: w, height: h)
        }
       
        deleteBtn.frame = CGRect(x: SCREENW - 5 - w, y: h * 2, width: w, height: h)
    }
    
    
    //MARK: - 懒加载删除按钮
    private lazy var deleteBtn: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: #selector(LXEmoticonPageViewCell.deleteBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        
        return button
    }()
    
    //MARK: - 删除按钮点击事件
    func deleteBtnClick(){
    
        NSNotificationCenter.defaultCenter().postNotificationName("deleteNoti", object: nil)
    }

}
